package com.zonski.jbots.engine;

import java.util.Vector;

/**
 * Describes a room
 */
public class Room
{
    public static final int DEFAULT_TILE_SIZE = 12;

    private QuickVector layers;
    private int[][] tiles;
    private int tileSize = DEFAULT_TILE_SIZE;
    private String music;
    private String name;

    public Room()
    {
        this.layers = new QuickVector(6);
    }
    public QuickVector getLayers()
    {
        return this.layers;
    }

    public int getTileType(int x, int y)
    {
        return this.tiles[y][x];
    }

    public void setTiles(int[][] tiles)
    {
        this.tiles = tiles;
    }

    public int getNumberOfTilesAcross()
    {
        return this.tiles[0].length;
    }

    public int getNumberOfTilesDown()
    {
        return this.tiles.length;
    }

    /**
     * The music for this room
     * @return the name of the music for this room
     */
    public String getMusic()
    {
        return this.music;
    }

    public void setMusic(String music)
    {
        this.music = music;
    }

    /**
     * The name of the room
     * @return the human readable name of the room
     */
    public String getName()
    {
        return this.name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public int getTileSize()
    {
        return this.tileSize;
    }

    public int getWidth()
    {
        return getTileSize() * getNumberOfTilesAcross();
    }

    public int getHeight()
    {
        return getTileSize() * getNumberOfTilesDown();
    }

    public void setTileSize(int tileSize)
    {
        this.tileSize = tileSize;
    }

    public void addEntity(Entity entity)
    {
        int layerId = entity.layerId;
        Layer foundLayer = null;
        //for(int i=this.layers.size(); i>0; )
        for(int i=0; i<layers.size(); i++)
        {
            //i--;
            Layer layer = (Layer)this.layers.elementAt(i);
            if(layer.getZOrder() == layerId)
            {
                foundLayer = layer;
                break;
            }else if(layer.getZOrder() > layerId){
                // we've passed the spot
                Layer newLayer = new Layer(layerId);
                this.layers.insertElementAt(newLayer, i);
                foundLayer = newLayer;
                break;
            }
        }
        if(foundLayer == null)
        {
            // create a new layer
            Layer layer = new Layer(layerId);
            this.layers.addElement(layer);
            foundLayer = layer;
        }
        foundLayer.getEntities().addElement(entity);
    }

    public void removeEntity(Entity entity)
    {
        int layerId = entity.layerId;
        Layer foundLayer = null;
        for(int i=this.layers.size(); i> 0; )
        {
            i--;
            Layer layer = (Layer)this.layers.elementAt(i);
            if(layer.getZOrder() == layerId)
            {
                foundLayer = layer;
                break;
            }else if(layer.getZOrder() < layerId){
                // we've passed the spot
                break;
            }
        }
        if(foundLayer != null)
        {
            foundLayer.getEntities().removeElement(entity);
        }
    }
}
